package edu.pku.sei.mdabase.infrastructure.metamodel;

import java.util.List;

import edu.pku.sei.mdabase.core.INamedElement;
import edu.pku.sei.mdabase.infrastructure.graph.Graph;

public class MetaModelGraph extends Graph<MetaClass,MetaReference> implements INamedElement {

	private String name;
	
	private Object host;

	public Object getHost() {
		return host;
	}

	public void setHost(Object host) {
		this.host = host;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}
	
	public MetaClass getMetaClass(String name){
		List<MetaClass> l = getNodes();
		
		for(MetaClass t : l){
			if(t.getName().equals(name))
				return t;
		}
		
		return null;
	}
	
	
	
	@Override
	public void addEdge(MetaReference edge, boolean propagation) {
		edge.computeMajor();
		super.addEdge(edge, propagation);
	}

	public MetaReference getMetaReference(String name, MetaClass source,MetaClass target){
		List<MetaReference> l = this.getEdges();
		
		for(MetaReference r : l ){
			if(name!=null&&name.length()!=0){
				if(name.equals(r.getName())==false)
					continue;
			}
			if(source!=null&&source.isA(r.getSource())==false){
				continue;
			}
			if(source!=null&&target.isA(r.getTarget())==false){
				continue;
			}
			
			return r;
		}
		return null;
	}
}
